<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.66">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <title>python-北理工 | 你好， VuePress ！</title><meta name="description" content="这是我的第一个 VuePress 站点">
    <link rel="preload" href="/assets/style-8a38cc2d.css" as="style"><link rel="stylesheet" href="/assets/style-8a38cc2d.css">
    <link rel="modulepreload" href="/assets/app-6c74e544.js"><link rel="modulepreload" href="/assets/python.html-da27b5e1.js"><link rel="modulepreload" href="/assets/python.html-80001bc2.js"><link rel="prefetch" href="/assets/index.html-75ca5b8c.js" as="script"><link rel="prefetch" href="/assets/化学.html-bcd92d03.js" as="script"><link rel="prefetch" href="/assets/IDEA.html-ce9ad873.js" as="script"><link rel="prefetch" href="/assets/python调用idm下载.html-615c23bf.js" as="script"><link rel="prefetch" href="/assets/index.html-571c49cc.js" as="script"><link rel="prefetch" href="/assets/VuePress2x搭建.html-910a2c4e.js" as="script"><link rel="prefetch" href="/assets/WindowsTerminal.html-4a75819f.js" as="script"><link rel="prefetch" href="/assets/乱七八糟.html-0ad5c8cc.js" as="script"><link rel="prefetch" href="/assets/国内访问GitHub.html-bd9bc6eb.js" as="script"><link rel="prefetch" href="/assets/虚拟机安装凤凰OS.html-fd472c84.js" as="script"><link rel="prefetch" href="/assets/软件工程过程.html-ad186a25.js" as="script"><link rel="prefetch" href="/assets/Java.html-a9e8e4b2.js" as="script"><link rel="prefetch" href="/assets/latex.html-b66b07c2.js" as="script"><link rel="prefetch" href="/assets/Markdown.html-76abef01.js" as="script"><link rel="prefetch" href="/assets/Markdown_vuepress.html-7a108572.js" as="script"><link rel="prefetch" href="/assets/Numpy.html-742cd1c8.js" as="script"><link rel="prefetch" href="/assets/python爬虫.html-0eaf5c34.js" as="script"><link rel="prefetch" href="/assets/index.html-404afaec.js" as="script"><link rel="prefetch" href="/assets/Redis.html-3c0347e6.js" as="script"><link rel="prefetch" href="/assets/Selenium.html-dd74eb14.js" as="script"><link rel="prefetch" href="/assets/快速幂算法 牛客小白月赛 1-C分元宵.html-6a00d92c.js" as="script"><link rel="prefetch" href="/assets/数据库.html-a9b825b9.js" as="script"><link rel="prefetch" href="/assets/数据结构_学校.html-db54f988.js" as="script"><link rel="prefetch" href="/assets/404.html-f9875e7b.js" as="script"><link rel="prefetch" href="/assets/index.html-f9be7961.js" as="script"><link rel="prefetch" href="/assets/化学.html-55546e87.js" as="script"><link rel="prefetch" href="/assets/IDEA.html-3d6ee1dc.js" as="script"><link rel="prefetch" href="/assets/python调用idm下载.html-862a9ae4.js" as="script"><link rel="prefetch" href="/assets/index.html-71e4b30e.js" as="script"><link rel="prefetch" href="/assets/VuePress2x搭建.html-69ed53cb.js" as="script"><link rel="prefetch" href="/assets/WindowsTerminal.html-d051c0b9.js" as="script"><link rel="prefetch" href="/assets/乱七八糟.html-e6e19f4d.js" as="script"><link rel="prefetch" href="/assets/国内访问GitHub.html-5a1e283c.js" as="script"><link rel="prefetch" href="/assets/虚拟机安装凤凰OS.html-3f19c5ba.js" as="script"><link rel="prefetch" href="/assets/软件工程过程.html-d5033030.js" as="script"><link rel="prefetch" href="/assets/Java.html-07b672b0.js" as="script"><link rel="prefetch" href="/assets/latex.html-139cf75a.js" as="script"><link rel="prefetch" href="/assets/Markdown.html-24c02676.js" as="script"><link rel="prefetch" href="/assets/Markdown_vuepress.html-a9f40501.js" as="script"><link rel="prefetch" href="/assets/Numpy.html-6ea20a5f.js" as="script"><link rel="prefetch" href="/assets/python爬虫.html-22e08aed.js" as="script"><link rel="prefetch" href="/assets/index.html-25c668e6.js" as="script"><link rel="prefetch" href="/assets/Redis.html-732c58b4.js" as="script"><link rel="prefetch" href="/assets/Selenium.html-8840df78.js" as="script"><link rel="prefetch" href="/assets/快速幂算法 牛客小白月赛 1-C分元宵.html-41e7e00f.js" as="script"><link rel="prefetch" href="/assets/数据库.html-e55ff260.js" as="script"><link rel="prefetch" href="/assets/数据结构_学校.html-cf35a9b9.js" as="script"><link rel="prefetch" href="/assets/404.html-ad53950d.js" as="script"><link rel="prefetch" href="/assets/mermaid-9e549946.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/" class=""><img class="logo" src="/img/hero.png" alt="你好， VuePress ！"><span class="site-name can-hide">你好， VuePress ！</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><div class="navbar-dropdown-wrapper"><button class="navbar-dropdown-title" type="button" aria-label="编程"><span class="title">编程</span><span class="arrow down"></span></button><button class="navbar-dropdown-title-mobile" type="button" aria-label="编程"><span class="title">编程</span><span class="right arrow"></span></button><ul style="display:none;" class="navbar-dropdown"><!--[--><li class="navbar-dropdown-item"><a href="/program/Java.md" class="" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a href="/program/Python.md" class="" aria-label="Python"><!--[--><!--]--> Python <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><!--[--><h4 class="navbar-dropdown-subtitle"><span>笔记</span></h4><ul class="navbar-dropdown-subitem-wrapper"><!--[--><li class="navbar-dropdown-subitem"><a href="/program/Markdown.html" class="" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a></li><li class="navbar-dropdown-subitem"><a href="/program/latex.html" class="" aria-label="Latex"><!--[--><!--]--> Latex <!--[--><!--]--></a></li><!--]--></ul><!--]--></li><li class="navbar-dropdown-item"><a href="/program/数据库.md" class="" aria-label="数据库"><!--[--><!--]--> 数据库 <!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-item"><a href="/hx/化学.md" class="" aria-label="化学"><!--[--><!--]--> 化学 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/others/" class="" aria-label="乱七八糟"><!--[--><!--]--> 乱七八糟 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="https://gitee.com/Francis-xsc/francis-xsc" rel="noopener noreferrer" target="_blank" aria-label="Gitee"><!--[--><!--]--> Gitee <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-color-mode-button" title="toggle color mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><div class="navbar-dropdown-wrapper"><button class="navbar-dropdown-title" type="button" aria-label="编程"><span class="title">编程</span><span class="arrow down"></span></button><button class="navbar-dropdown-title-mobile" type="button" aria-label="编程"><span class="title">编程</span><span class="right arrow"></span></button><ul style="display:none;" class="navbar-dropdown"><!--[--><li class="navbar-dropdown-item"><a href="/program/Java.md" class="" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a href="/program/Python.md" class="" aria-label="Python"><!--[--><!--]--> Python <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><!--[--><h4 class="navbar-dropdown-subtitle"><span>笔记</span></h4><ul class="navbar-dropdown-subitem-wrapper"><!--[--><li class="navbar-dropdown-subitem"><a href="/program/Markdown.html" class="" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a></li><li class="navbar-dropdown-subitem"><a href="/program/latex.html" class="" aria-label="Latex"><!--[--><!--]--> Latex <!--[--><!--]--></a></li><!--]--></ul><!--]--></li><li class="navbar-dropdown-item"><a href="/program/数据库.md" class="" aria-label="数据库"><!--[--><!--]--> 数据库 <!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-item"><a href="/hx/化学.md" class="" aria-label="化学"><!--[--><!--]--> 化学 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/others/" class="" aria-label="乱七八糟"><!--[--><!--]--> 乱七八糟 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="https://gitee.com/Francis-xsc/francis-xsc" rel="noopener noreferrer" target="_blank" aria-label="Gitee"><!--[--><!--]--> Gitee <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><a href="/program/Java.md" class="sidebar-item sidebar-heading collapsible" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a><!----></li><li><p tabindex="0" class="sidebar-item sidebar-heading active collapsible">Python <span class="down arrow"></span></p><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html" class="router-link-active router-link-exact-active router-link-active sidebar-item active" aria-label="python-北理工"><!--[--><!--]--> python-北理工 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#注释" class="router-link-active router-link-exact-active sidebar-item" aria-label="注释"><!--[--><!--]--> 注释 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#命名" class="router-link-active router-link-exact-active sidebar-item" aria-label="命名"><!--[--><!--]--> 命名 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#保留字" class="router-link-active router-link-exact-active sidebar-item" aria-label="保留字"><!--[--><!--]--> 保留字 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#列表" class="router-link-active router-link-exact-active sidebar-item" aria-label="列表"><!--[--><!--]--> 列表 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#print" class="router-link-active router-link-exact-active sidebar-item" aria-label="print"><!--[--><!--]--> print <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#eval" class="router-link-active router-link-exact-active sidebar-item" aria-label="eval"><!--[--><!--]--> eval <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#库引用" class="router-link-active router-link-exact-active sidebar-item" aria-label="库引用"><!--[--><!--]--> 库引用 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#" class="router-link-active router-link-exact-active sidebar-item" aria-label><!--[--><!--]-->  <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#整数类型" class="router-link-active router-link-exact-active sidebar-item" aria-label="整数类型"><!--[--><!--]--> 整数类型 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#四种进制表示形式" class="router-link-active router-link-exact-active sidebar-item" aria-label="四种进制表示形式"><!--[--><!--]--> 四种进制表示形式 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#浮点类型" class="router-link-active router-link-exact-active sidebar-item" aria-label="浮点类型"><!--[--><!--]--> 浮点类型 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#复数类型" class="router-link-active router-link-exact-active sidebar-item" aria-label="复数类型"><!--[--><!--]--> 复数类型 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#数值运算操作符" class="router-link-active router-link-exact-active sidebar-item" aria-label="数值运算操作符"><!--[--><!--]--> 数值运算操作符 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#数值运算函数" class="router-link-active router-link-exact-active sidebar-item" aria-label="数值运算函数"><!--[--><!--]--> 数值运算函数 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#函数" class="router-link-active router-link-exact-active sidebar-item" aria-label="函数"><!--[--><!--]--> 函数 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#字符串" class="router-link-active router-link-exact-active sidebar-item" aria-label="字符串"><!--[--><!--]--> 字符串 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#切片" class="router-link-active router-link-exact-active sidebar-item" aria-label="切片"><!--[--><!--]--> 切片 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#字符串操作符" class="router-link-active router-link-exact-active sidebar-item" aria-label="字符串操作符"><!--[--><!--]--> 字符串操作符 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#字符串处理函数" class="router-link-active router-link-exact-active sidebar-item" aria-label="字符串处理函数"><!--[--><!--]--> 字符串处理函数 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#format-方法的格式控制" class="router-link-active router-link-exact-active sidebar-item" aria-label="format()方法的格式控制"><!--[--><!--]--> format()方法的格式控制 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#time库" class="router-link-active router-link-exact-active sidebar-item" aria-label="time库"><!--[--><!--]--> time库 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#程序计时" class="router-link-active router-link-exact-active sidebar-item" aria-label="程序计时"><!--[--><!--]--> 程序计时 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#二分支结构" class="router-link-active router-link-exact-active sidebar-item" aria-label="二分支结构"><!--[--><!--]--> 二分支结构 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#条件组合" class="router-link-active router-link-exact-active sidebar-item" aria-label="条件组合"><!--[--><!--]--> 条件组合 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#异常处理" class="router-link-active router-link-exact-active sidebar-item" aria-label="异常处理"><!--[--><!--]--> 异常处理 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#多个输入" class="router-link-active router-link-exact-active sidebar-item" aria-label="多个输入"><!--[--><!--]--> 多个输入 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#循环" class="router-link-active router-link-exact-active sidebar-item" aria-label="循环"><!--[--><!--]--> 循环 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#遍历循环" class="router-link-active router-link-exact-active sidebar-item" aria-label="遍历循环"><!--[--><!--]--> 遍历循环 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#无限循环" class="router-link-active router-link-exact-active sidebar-item" aria-label="无限循环"><!--[--><!--]--> 无限循环 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#循环与else" class="router-link-active router-link-exact-active sidebar-item" aria-label="循环与else"><!--[--><!--]--> 循环与else <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#random库" class="router-link-active router-link-exact-active sidebar-item" aria-label="random库"><!--[--><!--]--> random库 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#函数-1" class="router-link-active router-link-exact-active sidebar-item" aria-label="函数"><!--[--><!--]--> 函数 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#可选参数" class="router-link-active router-link-exact-active sidebar-item" aria-label="可选参数"><!--[--><!--]--> 可选参数 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#可变参数传递" class="router-link-active router-link-exact-active sidebar-item" aria-label="可变参数传递"><!--[--><!--]--> 可变参数传递 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#lambda函数" class="router-link-active router-link-exact-active sidebar-item" aria-label="lambda函数"><!--[--><!--]--> lambda函数 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#集合" class="router-link-active router-link-exact-active sidebar-item" aria-label="集合"><!--[--><!--]--> 集合 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#定义" class="router-link-active router-link-exact-active sidebar-item" aria-label="定义"><!--[--><!--]--> 定义 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#集合是处理方法" class="router-link-active router-link-exact-active sidebar-item" aria-label="集合是处理方法"><!--[--><!--]--> 集合是处理方法 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#序列" class="router-link-active router-link-exact-active sidebar-item" aria-label="序列"><!--[--><!--]--> 序列 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#元组" class="router-link-active router-link-exact-active sidebar-item" aria-label="元组"><!--[--><!--]--> 元组 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#列表-1" class="router-link-active router-link-exact-active sidebar-item" aria-label="列表"><!--[--><!--]--> 列表 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#字典" class="router-link-active router-link-exact-active sidebar-item" aria-label="字典"><!--[--><!--]--> 字典 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#jieba库" class="router-link-active router-link-exact-active sidebar-item" aria-label="jieba库"><!--[--><!--]--> jieba库 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#文件" class="router-link-active router-link-exact-active sidebar-item" aria-label="文件"><!--[--><!--]--> 文件 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#文件的打开" class="router-link-active router-link-exact-active sidebar-item" aria-label="文件的打开"><!--[--><!--]--> 文件的打开 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#读取" class="router-link-active router-link-exact-active sidebar-item" aria-label="读取"><!--[--><!--]--> 读取 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#文件的全文本操作" class="router-link-active router-link-exact-active sidebar-item" aria-label="文件的全文本操作"><!--[--><!--]--> 文件的全文本操作 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#文件的逐行操作" class="router-link-active router-link-exact-active sidebar-item" aria-label="文件的逐行操作"><!--[--><!--]--> 文件的逐行操作 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#数据的文件写入" class="router-link-active router-link-exact-active sidebar-item" aria-label="数据的文件写入"><!--[--><!--]--> 数据的文件写入 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#wordcloud" class="router-link-active router-link-exact-active sidebar-item" aria-label="wordcloud"><!--[--><!--]--> wordcloud <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#requests" class="router-link-active router-link-exact-active sidebar-item" aria-label="requests"><!--[--><!--]--> requests <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/program/python.html#get" class="router-link-active router-link-exact-active sidebar-item" aria-label="get"><!--[--><!--]--> get <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#response对象的属性" class="router-link-active router-link-exact-active sidebar-item" aria-label="Response对象的属性"><!--[--><!--]--> Response对象的属性 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#requests库的7个主要方法" class="router-link-active router-link-exact-active sidebar-item" aria-label="Requests库的7个主要方法"><!--[--><!--]--> Requests库的7个主要方法 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/program/python.html#爬取网页的通用代码框架" class="router-link-active router-link-exact-active sidebar-item" aria-label="爬取网页的通用代码框架"><!--[--><!--]--> 爬取网页的通用代码框架 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/program/python.html#常见dos命令" class="router-link-active router-link-exact-active sidebar-item" aria-label="常见dos命令"><!--[--><!--]--> 常见dos命令 <!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul></li><li><p tabindex="0" class="sidebar-item sidebar-heading collapsible">Markdown <span class="right arrow"></span></p><ul style="display:none;" class="sidebar-item-children"><!--[--><li><a href="/program/Markdown.html" class="sidebar-item" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><!--]--><div><h1 id="python-北理工" tabindex="-1"><a class="header-anchor" href="#python-北理工" aria-hidden="true">#</a> python-北理工</h1><h2 id="注释" tabindex="-1"><a class="header-anchor" href="#注释" aria-hidden="true">#</a> 注释</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token comment">#单行注释</span>

<span class="token triple-quoted-string string">&#39;&#39;&#39;
多行注释
&#39;&#39;&#39;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="命名" tabindex="-1"><a class="header-anchor" href="#命名" aria-hidden="true">#</a> 命名</h2><p>大小写字母、数字、下划线和汉字等字符及组合</p><p>大小写敏感、首字符不能是数字、不与保留字相同</p><h2 id="保留字" tabindex="-1"><a class="header-anchor" href="#保留字" aria-hidden="true">#</a> 保留字</h2><p><img src="/img/program/python/image-20200815151230144.png" alt="image-20200815151230144"></p><p>切片：&lt;字符串&gt;[M:N]</p><p>M到N-1</p><h2 id="列表" tabindex="-1"><a class="header-anchor" href="#列表" aria-hidden="true">#</a> 列表</h2><p>由0个或多个数据组成的有序序列</p><p>-列表使用[]表示，采用逗号（,）分隔各元素</p><p>[&#39;F&#39;，&#39;f&#39;]表示两个元素&#39;F和&#39;f&#39;</p><p>使用保留字in判断一个元素是否在列表中</p><p>Tempstr[-1] in[&#39;C&#39;，&#39;c&#39;]判断前者是否与列表中某个元素相同</p><h2 id="print" tabindex="-1"><a class="header-anchor" href="#print" aria-hidden="true">#</a> print</h2><p>print(&quot;hsafn{:.2f}C&quot;.format(C))</p><p>小数点后两位</p><h2 id="eval" tabindex="-1"><a class="header-anchor" href="#eval" aria-hidden="true">#</a> eval</h2><p>去掉参数最外侧引号并执行余下语句的函数</p><p>eval(&quot;1&quot;)</p><p>eval(&quot;1+2&quot;)</p><h2 id="库引用" tabindex="-1"><a class="header-anchor" href="#库引用" aria-hidden="true">#</a> 库引用</h2><p>import&lt;库名&gt;</p><p>&lt;库名&gt;.&lt;函数名&gt;(&lt;函数参数&gt;)</p><p>from&lt;库名&gt;import&lt;函数名&gt;或import</p><p><img src="/img/program/python/image-20200815160656918.png" alt="image-20200815160656918"></p><p>&lt;函数名&gt;(&lt;函数参数&gt;)</p><p>import&lt;库名&gt;as&lt;库别名&gt;</p><p>&lt;库别名&gt;.&lt;函数名&gt;(&lt;函数参数&gt;)</p><h2 id="" tabindex="-1"><a class="header-anchor" href="#" aria-hidden="true">#</a></h2><p>range(N)</p><p>0-------N</p><p>range(M,N)</p><p>M------N-1</p><h2 id="整数类型" tabindex="-1"><a class="header-anchor" href="#整数类型" aria-hidden="true">#</a> 整数类型</h2><h3 id="四种进制表示形式" tabindex="-1"><a class="header-anchor" href="#四种进制表示形式" aria-hidden="true">#</a> 四种进制表示形式</h3><ul><li>十进制</li><li>二进制，以0b或0B开头</li><li>八进制，以0o或0O开头</li><li>十六进制，以0x或0X开头</li></ul><h3 id="浮点类型" tabindex="-1"><a class="header-anchor" href="#浮点类型" aria-hidden="true">#</a> 浮点类型</h3><p>浮点数间运算存在不确定尾数，不是bug</p><p>0.1+0.2=0.30000000000000004</p><p><code>round(x,d)</code>对x四舍五入，d是小数截取位数</p><p>浮点数运算几比较用round()函数辅助</p><p>科学计数法<code>&lt;a&gt;e&lt;b&gt;</code>表示a*10b</p><p>4.3e-3------0.0043</p><p>9.6E5-------960000.0</p><h2 id="复数类型" tabindex="-1"><a class="header-anchor" href="#复数类型" aria-hidden="true">#</a> 复数类型</h2><p>z = 1.23e-4 + 5.6e+89j</p><p><code>z.real</code>获得实部</p><p><code>z.imag</code>获得虚部</p><h2 id="数值运算操作符" tabindex="-1"><a class="header-anchor" href="#数值运算操作符" aria-hidden="true">#</a> 数值运算操作符</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>x<span class="token operator">+</span>y
x<span class="token operator">-</span>y
x<span class="token operator">*</span>y
x<span class="token operator">/</span>y		结果为浮点数
x<span class="token operator">//</span>y	<span class="token operator">//</span>	整数除。x与y之整数商	<span class="token number">10</span><span class="token operator">//</span><span class="token number">3</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token number">3</span>
x<span class="token operator">%</span>y
x<span class="token operator">**</span>y	x <span class="token punctuation">(</span>y<span class="token punctuation">)</span>
二元操作符
x<span class="token operator">+=</span>y
x<span class="token operator">-=</span>y
x<span class="token operator">*=</span>y
x<span class="token operator">/=</span>y		结果为浮点数
x<span class="token operator">//=</span>y	<span class="token operator">//</span>	整数除。x与y之整数商	<span class="token number">10</span><span class="token operator">//</span><span class="token number">3</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token number">3</span>
x<span class="token operator">%=</span>y
x<span class="token operator">**=</span>y
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数值运算函数" tabindex="-1"><a class="header-anchor" href="#数值运算函数" aria-hidden="true">#</a> 数值运算函数</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token builtin">abs</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
<span class="token builtin">divmod</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>	<span class="token comment">#商余，(x//y,x%y)同时输出商和余数，divmod(10,3)结果为(3,1)</span>
<span class="token builtin">pow</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">[</span><span class="token punctuation">,</span>z<span class="token punctuation">]</span><span class="token punctuation">)</span>	<span class="token comment">#幂余</span>
<span class="token builtin">round</span><span class="token punctuation">(</span>x<span class="token punctuation">[</span><span class="token punctuation">,</span>d<span class="token punctuation">]</span><span class="token punctuation">)</span>	<span class="token comment">#d默认值为0</span>
<span class="token builtin">max</span>（x1<span class="token punctuation">,</span>x2<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>xn<span class="token punctuation">)</span>	<span class="token comment">#不限个数</span>
<span class="token builtin">min</span>

<span class="token builtin">int</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>	<span class="token builtin">int</span><span class="token punctuation">(</span><span class="token number">123.45</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token number">123</span>	<span class="token builtin">int</span><span class="token punctuation">(</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token number">123</span>
<span class="token builtin">float</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
comples<span class="token punctuation">(</span>x<span class="token punctuation">)</span>	<span class="token comment">#将x变为复数，增加虚数部分</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="函数" tabindex="-1"><a class="header-anchor" href="#函数" aria-hidden="true">#</a> 函数</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">dayup</span><span class="token punctuation">(</span>df<span class="token punctuation">)</span><span class="token punctuation">:</span>
    dayup<span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">365</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> i<span class="token operator">%</span><span class="token number">7</span> <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
            dayup <span class="token operator">=</span> dayup <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">-</span> <span class="token number">0.01</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            dayup <span class="token operator">=</span> dayup <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">+</span> df<span class="token punctuation">)</span>
     <span class="token keyword">return</span> dayup
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="字符串" tabindex="-1"><a class="header-anchor" href="#字符串" aria-hidden="true">#</a> 字符串</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>一对单引号或双引号。单行字符串
<span class="token string">&quot;oipsafuj&quot;</span>
<span class="token string">&#39;c&#39;</span>
由一对三单引号或三双引号表示，可表示多行字符串
<span class="token triple-quoted-string string">&#39;&#39;&#39;
alks;jfh
kljsa
sa
&#39;&#39;&#39;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="切片" tabindex="-1"><a class="header-anchor" href="#切片" aria-hidden="true">#</a> 切片</h3><p>&lt;字符串&gt;[M:N]，M缺失表示至开头，N缺失表示至结尾</p><p>&lt;字符串&gt;[M:N:K]，根据步长K对字符串切片</p><p>&quot;O一二三四五六七八九十&quot;[1:8:2]结果是“一三五七&quot;</p><p>&#39;o一二三四五六七八九十&quot;[::-1]结果是&quot;十九八七六五四三二一o&quot;</p><h3 id="字符串操作符" tabindex="-1"><a class="header-anchor" href="#字符串操作符" aria-hidden="true">#</a> 字符串操作符</h3><table><thead><tr><th>操作符及其使用</th><th>描述</th></tr></thead><tbody><tr><td>x+y</td><td>连接</td></tr><tr><td>n<em>x或x</em>n</td><td>复制n次字符串x</td></tr><tr><td>x in s</td><td>x是s的字串</td></tr></tbody></table><h3 id="字符串处理函数" tabindex="-1"><a class="header-anchor" href="#字符串处理函数" aria-hidden="true">#</a> 字符串处理函数</h3><p>len(x)</p><p>str(x) 任意类型x所对应的字符串形式</p><p>str(1.23)-----&gt;&quot;1.23&quot; str([1,2])--------&gt;&quot;[1,2]&quot;</p><p>hex(x) oct(x)</p><p><img src="/img/program/python/image-20200816010712885.png" alt="image-20200816010712885"></p><p>str.lower()</p><p>str.upper()</p><p>str.split(sep=None) 返回一个列表，由str根据sep被分隔的部分组成</p><p>&quot;A,B,C&quot;.split(&quot;,&quot;)结果为[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;]</p><p>str.count(sub)</p><p>str.replace(old,new)</p><p>str.center(width,[,fillchar])字符串str根据宽度width居中，fillchar可选</p><p>str.strip(chars) 从str中去掉在其左侧和右侧chars中列出的字符</p><p>str.join(iter) 在iter变量除最后元素外每个元素后增加一个str</p><h2 id="format-方法的格式控制" tabindex="-1"><a class="header-anchor" href="#format-方法的格式控制" aria-hidden="true">#</a> format()方法的格式控制</h2><p><img src="/img/program/python/image-20200816090153326.png" alt="image-20200816090153326"></p><p><img src="/img/program/python/image-20200816090414619.png" alt="image-20200816090414619"></p><p><img src="/img/program/python/image-20200816090959802.png" alt="image-20200816090959802"></p><h2 id="time库" tabindex="-1"><a class="header-anchor" href="#time库" aria-hidden="true">#</a> time库</h2><p>time() 获取当前时间戳，浮点数 time.time()</p><p>ctime() 获取当前时间并以易读方式表示，返回字符串</p><p>gmtime() 获取当前时间，表示为计算机可处理的时间格式</p><p>strftime(tpl,ts) tpl是格式化模板字符串，用来定义输出效果 ts是计算机内部时间类型变量</p><p>t= time.gmtime() time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;,t)</p><table><thead><tr><th>格式化字符串</th><th>说明</th><th>举例</th></tr></thead><tbody><tr><td>%Y</td><td>年份</td><td>0000-9999</td></tr><tr><td>%m</td><td>月份</td><td>01-12</td></tr><tr><td>%B</td><td>月份名称</td><td>January-December</td></tr><tr><td>%b</td><td>月份名称缩写</td><td>Jan-Dec</td></tr><tr><td>%d</td><td>日期</td><td>01-31</td></tr><tr><td>%A</td><td>星期</td><td>Monday-Sunday</td></tr><tr><td>%a</td><td>星期缩写</td><td>Mon-Sun</td></tr><tr><td>%H</td><td>小时（24h制）</td><td>00-23</td></tr><tr><td>%I</td><td>小时（12h制）</td><td>01-12</td></tr><tr><td>%p</td><td>上下午</td><td>AM,PM</td></tr><tr><td>%M</td><td>分钟</td><td>00-59</td></tr><tr><td>%S</td><td>秒</td><td>00-59</td></tr></tbody></table><p><img src="/img/program/python/image-20200816095438581.png" alt="image-20200816095438581"></p><h3 id="程序计时" tabindex="-1"><a class="header-anchor" href="#程序计时" aria-hidden="true">#</a> 程序计时</h3><p><img src="/img/program/python/image-20200816095709044.png" alt="image-20200816095709044"></p><p><img src="/img/program/python/image-20200816095817550.png" alt="image-20200816095817550"></p><p>\r 光标回到行首】</p><h3 id="二分支结构" tabindex="-1"><a class="header-anchor" href="#二分支结构" aria-hidden="true">#</a> 二分支结构</h3><p>&lt;表达式1&gt; if &lt;条件&gt;else&lt;&gt;</p><h2 id="条件组合" tabindex="-1"><a class="header-anchor" href="#条件组合" aria-hidden="true">#</a> 条件组合</h2><p>and or not</p><h2 id="异常处理" tabindex="-1"><a class="header-anchor" href="#异常处理" aria-hidden="true">#</a> 异常处理</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">1</span><span class="token operator">&gt;</span>
<span class="token keyword">except</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">2</span><span class="token operator">&gt;</span>
    
<span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">1</span><span class="token operator">&gt;</span>
<span class="token keyword">except</span> <span class="token operator">&lt;</span>异常类型<span class="token operator">&gt;</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">2</span><span class="token operator">&gt;</span>
    
<span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">1</span><span class="token operator">&gt;</span>
<span class="token keyword">except</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">2</span><span class="token operator">&gt;</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">3</span><span class="token operator">&gt;</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token number">4</span><span class="token operator">&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="多个输入" tabindex="-1"><a class="header-anchor" href="#多个输入" aria-hidden="true">#</a> 多个输入</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token operator">=</span><span class="token builtin">eval</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token operator">+</span>c<span class="token punctuation">)</span>
输入时用逗号分隔
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="循环" tabindex="-1"><a class="header-anchor" href="#循环" aria-hidden="true">#</a> 循环</h2><h3 id="遍历循环" tabindex="-1"><a class="header-anchor" href="#遍历循环" aria-hidden="true">#</a> 遍历循环</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">for</span> <span class="token operator">&lt;</span>循环变量<span class="token operator">&gt;</span> <span class="token keyword">in</span> <span class="token operator">&lt;</span>遍历结构<span class="token operator">&gt;</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>语句块<span class="token operator">&gt;</span>

    
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span>
    
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>M<span class="token punctuation">,</span>N<span class="token punctuation">,</span>K<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token number">12345</span>
<span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token number">135</span>

字符串遍历循环
<span class="token keyword">for</span> c <span class="token keyword">in</span> s<span class="token punctuation">:</span>
    
列表遍历循环
<span class="token keyword">for</span> item <span class="token keyword">in</span> ls<span class="token punctuation">:</span>
ls是一个列表

文件遍历循环
<span class="token keyword">for</span> line <span class="token keyword">in</span> fi<span class="token punctuation">:</span>
fi是一个文件标识符，遍历其每行  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="无限循环" tabindex="-1"><a class="header-anchor" href="#无限循环" aria-hidden="true">#</a> 无限循环</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">while</span> <span class="token operator">&lt;</span>条件<span class="token operator">&gt;</span><span class="token punctuation">:</span>
	<span class="token operator">&lt;</span>语句块<span class="token operator">&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="循环与else" tabindex="-1"><a class="header-anchor" href="#循环与else" aria-hidden="true">#</a> 循环与else</h3><p>当循环没有被break语句退出时，执行else语句块</p><p>else语句块作为&quot;正常&quot;完成循环的奖励</p><h2 id="random库" tabindex="-1"><a class="header-anchor" href="#random库" aria-hidden="true">#</a> random库</h2><table><thead><tr><th>函数</th><th>功能</th></tr></thead><tbody><tr><td>seed(a=None)</td><td>初始化给定的随机数种子，默认为当前系统时间random(10)</td></tr><tr><td>random()</td><td>生成一个[0.0,1.0)之间的随机小数</td></tr><tr><td>randint(a,b)</td><td>生成一个[a,b]之间的整数</td></tr><tr><td>randrange(m,n[,k])</td><td>生成一个[m, n)之间以k为步长的随机整数</td></tr><tr><td>getrandbits(k)</td><td>生成一个k比特长的随机整数</td></tr><tr><td>uniform(a,b)</td><td>生成一个[a,b]之间的随机小数</td></tr><tr><td>choice(seq)</td><td>从序列seq中随机选择一个元素</td></tr><tr><td>shuffle(seq)</td><td>将序列seq中元素随机排列，返回打乱后的序列</td></tr></tbody></table><p>分行 增加\</p><h2 id="函数-1" tabindex="-1"><a class="header-anchor" href="#函数-1" aria-hidden="true">#</a> 函数</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token operator">&lt;</span>函数名<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token operator">&lt;</span>参数<span class="token punctuation">(</span><span class="token number">0</span>个或多个<span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>函数体<span class="token operator">&gt;</span>
    <span class="token keyword">return</span> <span class="token operator">&lt;</span>返回值<span class="token operator">&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可选参数" tabindex="-1"><a class="header-anchor" href="#可选参数" aria-hidden="true">#</a> 可选参数</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">fact</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span>m<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    s<span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        s<span class="token operator">*=</span>i
    <span class="token keyword">return</span> s<span class="token operator">//</span>m
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可变参数传递" tabindex="-1"><a class="header-anchor" href="#可变参数传递" aria-hidden="true">#</a> 可变参数传递</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token operator">&lt;</span>函数名<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token operator">&lt;</span>参数<span class="token operator">&gt;</span><span class="token punctuation">,</span><span class="token operator">*</span>b<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token operator">&lt;</span>函数体<span class="token operator">&gt;</span>
    <span class="token keyword">return</span> <span class="token operator">&lt;</span>返回值<span class="token operator">&gt;</span>


<span class="token keyword">def</span> <span class="token function">fact</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span><span class="token operator">*</span>b<span class="token punctuation">)</span><span class="token punctuation">:</span>
    s <span class="token operator">=</span> <span class="token number">1</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">.</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        s <span class="token operator">*=</span> i
    <span class="token keyword">for</span> item <span class="token keyword">in</span> b<span class="token punctuation">:</span>
        s <span class="token operator">*=</span> item
    <span class="token keyword">return</span> s
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>函数调用时，参数可以按照位置或名称方式传递</p><p><img src="/img/program/python/image-20200816222710281.png" alt="image-20200816222710281"></p><p>声明global 使用全局变量</p><h3 id="lambda函数" tabindex="-1"><a class="header-anchor" href="#lambda函数" aria-hidden="true">#</a> lambda函数</h3><p>&lt;函数名&gt; = lambda &lt;参数&gt; : &lt;表达式&gt;</p><h2 id="集合" tabindex="-1"><a class="header-anchor" href="#集合" aria-hidden="true">#</a> 集合</h2><h3 id="定义" tabindex="-1"><a class="header-anchor" href="#定义" aria-hidden="true">#</a> 定义</h3><ul><li>用{}表示，元素间用逗号分隔</li><li>建立集合类型用{}或set()</li><li>建立空集合类型，必须使用set()</li></ul><table><thead><tr><th>操作符</th><th>描述</th></tr></thead><tbody><tr><td>S|T</td><td>并S|=T</td></tr><tr><td>s-T</td><td>S-=T</td></tr><tr><td>S &amp; T</td><td>交S&amp;=T</td></tr><tr><td>S ^ T</td><td>补，非相同元素S^=T</td></tr><tr><td>S &lt;= T或S &lt; T</td><td></td></tr><tr><td>S &gt;= T或 S &gt; T</td><td></td></tr></tbody></table><h3 id="集合是处理方法" tabindex="-1"><a class="header-anchor" href="#集合是处理方法" aria-hidden="true">#</a> 集合是处理方法</h3><table><thead><tr><th>操作函数或方法</th><th>描述</th></tr></thead><tbody><tr><td>S.add(x)</td><td>如果x不在集合S中，将x增加到S</td></tr><tr><td>S.dicard(x)</td><td>移除S中元素x，如果x不在集合S中，不报错</td></tr><tr><td>S.remove(x)</td><td>移除S中元素x，如果x不在集合S中，产生KeyError异常</td></tr><tr><td>S.clear()</td><td>移除S中所有元素</td></tr><tr><td>S.pop()</td><td>随机返回S的一个元素，更新S，若S为空产生KeyError异常</td></tr><tr><td>S.copy()</td><td>返回集合S的一个副本</td></tr><tr><td>len(S)</td><td>返回集合S的元素个数</td></tr><tr><td>x in S</td><td>判断S中元素x，x在集合S中，返回True，否则返回False</td></tr><tr><td>x not in S</td><td>判断S中元素x，x不在集合S中，返回True，否则返回False</td></tr><tr><td>set(x)</td><td>将其他类型变量x转变为集合类型</td></tr></tbody></table><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>A <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&quot;p&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;y&quot;</span><span class="token punctuation">,</span><span class="token number">123</span><span class="token punctuation">}</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> A<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span>end<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="序列" tabindex="-1"><a class="header-anchor" href="#序列" aria-hidden="true">#</a> 序列</h2><ul><li>操作符 <ul><li>x in s</li><li>x not in s</li><li>s + t</li><li>s* n/n * s</li><li>s[i]</li><li>s[i:j] / s[i : j : k]</li></ul></li><li>函数和方法 <ul><li>len(s)</li><li>min(s)</li><li>max(s)</li><li>==s.index(x)或s.index(x,i,j)== 返回x在x中索引</li></ul></li></ul><h3 id="元组" tabindex="-1"><a class="header-anchor" href="#元组" aria-hidden="true">#</a> 元组</h3><ul><li>使用()或tuple()创建，</li><li>不能修改</li></ul><h3 id="列表-1" tabindex="-1"><a class="header-anchor" href="#列表-1" aria-hidden="true">#</a> 列表</h3><ul><li>可修改</li><li>使用[]或list()创建</li></ul><table><thead><tr><th>函数或方法</th><th>描述</th></tr></thead><tbody><tr><td>ls[i]=x</td><td>替换列表ls第i元素为x</td></tr><tr><td>ls[i:j:k]=lt</td><td>用列表lt替换ls切片后所对应元素子列表</td></tr><tr><td>del ls[i]</td><td>删除列表ls中第i元素</td></tr><tr><td>del ls[i:j:k]</td><td>删除列表ls中第i到第j以k为步长的元素</td></tr><tr><td>ls += lt</td><td>更新列表ls，将列表lt元素增加到列表ls中</td></tr><tr><td>ls *= n</td><td>更新列表ls，其元素重复n次</td></tr><tr><td>ls.append(x)</td><td>在列表ls最后增加一个元素x</td></tr><tr><td>ls.clear()</td><td>删除列表ls中所有元素</td></tr><tr><td>ls.copy()</td><td>生成一个新列表，赋值ls中所有元素</td></tr><tr><td>ls.insert(i,x)</td><td>在列表ls的第i位置增加元素x</td></tr><tr><td>ls.pop(i)</td><td>将列表ls中第i位置元素取出并删除该元素</td></tr><tr><td>ls.remove(x)</td><td>将列表ls中出现的第一个元素x删除</td></tr><tr><td>ls.reverse()</td><td>将列表ls中的元素反转</td></tr></tbody></table><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token comment">#获取用户不定长度的输入</span>
<span class="token keyword">def</span> <span class="token function">getNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    nums<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    iNumStr<span class="token operator">=</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入数字（回车退出）&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">while</span> iNumstr<span class="token operator">!=</span> “”<span class="token punctuation">:</span>
        nums<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token builtin">eval</span><span class="token punctuation">(</span>iNumStr<span class="token punctuation">)</span><span class="token punctuation">)</span>
        iNumStr<span class="token operator">=</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入数字（回车退出）&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> nums
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="字典" tabindex="-1"><a class="header-anchor" href="#字典" aria-hidden="true">#</a> 字典</h2><p>de = {};</p><table><thead><tr><th>函数或方法</th><th>描述</th></tr></thead><tbody><tr><td>del d[k]</td><td>删除字典d中键k对应的数据值</td></tr><tr><td>k in d</td><td>判断键k是否在字典d中，如果在返回True，否则False</td></tr><tr><td>d.keys()</td><td>返回字典d中所有的键信息</td></tr><tr><td>d.values()</td><td>返回字典d中所有的值信息</td></tr><tr><td>d.get(k,)</td><td>键k存在，则返回相应值，不在则返回值</td></tr><tr><td>d.get(k,)</td><td>键k存在，则取出相应值，不在则返回值</td></tr><tr><td>d.popitem()</td><td>随机从字典d中取出一个键值对，以元组形式返回</td></tr><tr><td>d.clear()</td><td>删除所有的键值对</td></tr></tbody></table><h2 id="jieba库" tabindex="-1"><a class="header-anchor" href="#jieba库" aria-hidden="true">#</a> jieba库</h2><ul><li>精确模式:把文本精确的切分开，不存在冗余单词</li><li>全模式:把文本中所有可能的词语都扫描出来，有冗余</li><li>搜索引擎模式∶在精确模式基础上，对长词再次切分</li></ul><table><thead><tr><th>函数</th><th>描述</th></tr></thead><tbody><tr><td>jieba.lcuts(s)</td><td>精确模式，返回一个列表类型的分词结果</td></tr><tr><td>jieba.lcuts(s,cut_all=True)</td><td>全模式，返回一个列表类型的分词结果，存在冗余</td></tr><tr><td>jieba.lcut_for_search(s)</td><td>搜索引擎模式，返回一个列表类型的分词结果，存在冗余</td></tr><tr><td>jieba.add_word(w)</td><td>向分词词典增加新词w</td></tr></tbody></table><h2 id="文件" tabindex="-1"><a class="header-anchor" href="#文件" aria-hidden="true">#</a> 文件</h2><ul><li>a=open(,)</li><li>读文件 <ul><li>a.read(size)</li><li>a.readline(size)</li><li>a.readlines(hint)</li></ul></li><li>写文件 <ul><li>a.write(s)</li><li>a.writelines(lines)</li><li>a.seek(offset)</li></ul></li><li>a.close()</li></ul><h3 id="文件的打开" tabindex="-1"><a class="header-anchor" href="#文件的打开" aria-hidden="true">#</a> 文件的打开</h3><p>&lt;变量名&gt; = open(&lt;文件名&gt;,&lt;打开模式&gt;)</p><p>文件名：文件路径和名称</p><ul><li>D:/asd/fgh或D:\asd\fgh</li><li>./asd/asd</li></ul><p>打开模式：文本/二进制</p><table><thead><tr><th>文件的打开模式</th><th>描述</th></tr></thead><tbody><tr><td>&#39;r&#39;</td><td>只读模式，默认值，如果文件不存在，返回FileNotFoundError</td></tr><tr><td>&#39;w&#39;</td><td>覆盖写模式，文件不存在则创建，存在则完全覆盖</td></tr><tr><td>&#39;x&#39;</td><td>创建写模式，文件不存在则创建，存在则返回FileExistsError</td></tr><tr><td>&#39;a&#39;</td><td>追加写模式，文件不存在则创建，存在则在文件最后追加内容</td></tr><tr><td>&#39;b&#39;</td><td>二进制文件模式</td></tr><tr><td>&#39;t&#39;</td><td>文本文件模式，默认值</td></tr><tr><td>‘+’</td><td>与r/w/x/a一同使用，在原功能基础上增加同时读写功能</td></tr></tbody></table><h3 id="读取" tabindex="-1"><a class="header-anchor" href="#读取" aria-hidden="true">#</a> 读取</h3><table><thead><tr><th>操作方法</th><th>描述</th></tr></thead><tbody><tr><td><code>&lt;f&gt;.read(size=-1)</code></td><td>读入全部内容，如果给出参数，读入前size长度</td></tr><tr><td><code>&lt;f&gt;.readline(size=-1)</code></td><td>读入一行内容，如果给出参数，读入该行前size长度</td></tr><tr><td><code>&lt;f&gt;.readlines(size=-1)</code></td><td>读入文件所有行，以每行为元素形成列表 如果给出参数，读入前hint行</td></tr></tbody></table><h3 id="文件的全文本操作" tabindex="-1"><a class="header-anchor" href="#文件的全文本操作" aria-hidden="true">#</a> 文件的全文本操作</h3><p>遍历全文本：方法一</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>fname <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入要打开的文件名称：&quot;</span><span class="token punctuation">)</span>
fo <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span><span class="token string">&quot;r&quot;</span><span class="token punctuation">)</span>
txt<span class="token operator">=</span>fo<span class="token punctuation">.</span>read
<span class="token comment">#对全文txt进行处理</span>
fo<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>遍历全文本：方法二</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>fname <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入要打开的文件名称：&quot;</span><span class="token punctuation">)</span>
fo <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span><span class="token string">&quot;r&quot;</span><span class="token punctuation">)</span>
txt<span class="token operator">=</span>fo<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword">while</span> txt <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">:</span>
    <span class="token comment">#对txt进行处理</span>
    txt <span class="token operator">=</span> fo<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
fo<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件的逐行操作" tabindex="-1"><a class="header-anchor" href="#文件的逐行操作" aria-hidden="true">#</a> 文件的逐行操作</h3><p>逐行遍历文件：方法一</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>fname <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入要打开的文件名称：&quot;</span><span class="token punctuation">)</span>
fo <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span><span class="token string">&quot;r&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> line <span class="token keyword">in</span> fo<span class="token punctuation">.</span>readlines<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
fo<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>   
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>逐行遍历文件：方法二</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>fname <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">&quot;请输入要打开的文件名称：&quot;</span><span class="token punctuation">)</span>
fo <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span><span class="token string">&quot;r&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> line <span class="token keyword">in</span> fo<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
fo<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>   
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="数据的文件写入" tabindex="-1"><a class="header-anchor" href="#数据的文件写入" aria-hidden="true">#</a> 数据的文件写入</h3><table><thead><tr><th>操作方法</th><th>描述</th></tr></thead><tbody><tr><td>.write(s)</td><td>向文件写入一个字符串或字节流</td></tr><tr><td>.writelines(lines)</td><td>将一个元素全为字符串的列表写入文件</td></tr><tr><td>.seek(offset)</td><td>改变当前文件操作指针的位置，offset含义如下: 0一文件开头;1–当前位置;2–文件结尾</td></tr></tbody></table><p>采用空格分隔方式将数据写入文件</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>ls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&#39;中国&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;美国&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;日本&#39;</span><span class="token punctuation">]</span>
f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>fname<span class="token punctuation">,</span><span class="token string">&#39;w&#39;</span><span class="token punctuation">)</span>
f<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>ls<span class="token punctuation">)</span><span class="token punctuation">)</span>
f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="wordcloud" tabindex="-1"><a class="header-anchor" href="#wordcloud" aria-hidden="true">#</a> wordcloud</h3><p>w=wordcloud.WordCloud(参数)</p><table><thead><tr><th>参数</th><th>描述</th></tr></thead><tbody><tr><td>width</td><td>指定词云对象生成图片的宽度，默认400像素 w=wordclod.WordCloud(width=600)</td></tr><tr><td>height</td><td>指定词云对象生成图片的高度，默认600像素</td></tr><tr><td>min_font_size</td><td>指定词云中字体的最小字号，默认4号</td></tr><tr><td>max_font_size</td><td>指定词云中字体的最大字号，根据高度自动调节</td></tr></tbody></table><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>w.generate(txt)</td><td>向WordCloud对象w中加载文本txt,</td></tr><tr><td>w.to_file(filename)</td><td>将词云输出为图像文件，.png或.jpg格式</td></tr><tr><td>font_step</td><td>指定词云中字体字号的步进间隔，默认为1</td></tr><tr><td>font_path</td><td>指定字体文件的路径，默认None w=wordcloud.wordCloud(font_path=&quot;msyh.ttc&quot;)</td></tr><tr><td>max_words</td><td>指定词云显示的最大单词数量，默认200</td></tr><tr><td>stop_words</td><td>指定词云的排除词列表，即不显示的单词列表 w=wordcloud.WordCloud(stop_words={&quot;Python&quot;})</td></tr><tr><td>background_color</td><td>指定词云图片的背景颜色，默认为黑色</td></tr><tr><td>mask</td><td>指定词云形状，默认为长方形，需要引用imread()函数 &gt;&gt;&gt;from scipy.misc import imread &gt;&gt;&gt;mk=imread(&quot;pic.png&quot;) &gt;&gt;&gt;W=wordcloud. WordCloud(mask=mk)</td></tr></tbody></table><h1 id="爬虫" tabindex="-1"><a class="header-anchor" href="#爬虫" aria-hidden="true">#</a> 爬虫</h1><h2 id="requests" tabindex="-1"><a class="header-anchor" href="#requests" aria-hidden="true">#</a> requests</h2><h3 id="get" tabindex="-1"><a class="header-anchor" href="#get" aria-hidden="true">#</a> get</h3><p>requests.get(url,params,**kwargs)</p><p>url:拟获取页面的url链接</p><p>params:url中的额外参数，字典或字节流格式，可选</p><p>*kwargs:12个控制访问的参数</p><h3 id="response对象的属性" tabindex="-1"><a class="header-anchor" href="#response对象的属性" aria-hidden="true">#</a> Response对象的属性</h3><table><thead><tr><th>属性</th><th>说明</th></tr></thead><tbody><tr><td>r.status_code</td><td>HTTP请求的返回状态，200表示连接成功，404表示失败</td></tr><tr><td>r.text</td><td>HTTP响应内容的字符串形式，即，url对应的页面内容</td></tr><tr><td>r.encoding</td><td>从HTTP header中猜测的响应内容编码方式</td></tr><tr><td>r.apparent_encoding</td><td>从内容中分析出的响应内容编码方式（备选编码方式）</td></tr><tr><td>r.content</td><td>HTTP响应内容的二进制形式</td></tr></tbody></table><p>r.raise_for_status()如果不是200，产生异常requests.HTTPError</p><h3 id="requests库的7个主要方法" tabindex="-1"><a class="header-anchor" href="#requests库的7个主要方法" aria-hidden="true">#</a> Requests库的7个主要方法</h3><table><thead><tr><th>方法</th><th>说明</th></tr></thead><tbody><tr><td>requests.requests()</td><td>构造一个请求，支撑以下各方法的基础方法</td></tr><tr><td>requests.get()</td><td>获取HTML网页的主要方法，对应于HTTP的GET</td></tr><tr><td>requests.head()</td><td>获取HTML网页头信息的方法，对应于HTTP的HEAD</td></tr><tr><td>requests.post()</td><td>向HTML网页提交POST请求的方法，对应于HTTP的POST</td></tr><tr><td>requests.put()</td><td>向HTML网页提交PUT请求的方法，对应于HTTP的PUT</td></tr><tr><td>requests.patch()</td><td>向HTML网页提交局部修改请求，对应于HTTP的PATCH</td></tr><tr><td>requests.delete()</td><td>向HTML页面提交删除请求，对应于HTTP的DELETE</td></tr><tr><td></td><td></td></tr></tbody></table><h2 id="爬取网页的通用代码框架" tabindex="-1"><a class="header-anchor" href="#爬取网页的通用代码框架" aria-hidden="true">#</a> 爬取网页的通用代码框架</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> requests
<span class="token keyword">def</span> <span class="token function">getHTMLText</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        r <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span>timeout<span class="token operator">=</span><span class="token number">30</span><span class="token punctuation">)</span>
        r<span class="token punctuation">.</span>raise_for_status<span class="token punctuation">(</span><span class="token punctuation">)</span>
        r<span class="token punctuation">.</span>encoding <span class="token operator">=</span> r<span class="token punctuation">.</span>apparent_encoding
        <span class="token keyword">return</span> r<span class="token punctuation">.</span>text
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token string">&quot;产生异常&quot;</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    url<span class="token operator">=</span><span class="token string">&quot;http://www.baidu.com&quot;</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>getHTMLText<span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#获取一个get请求</span>
response<span class="token operator">=</span>urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlopen<span class="token punctuation">(</span><span class="token string">&quot;http://www.baidu.com&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#获取一个post请求</span>
<span class="token keyword">import</span> urllib<span class="token punctuation">.</span>parse
data <span class="token operator">=</span> <span class="token builtin">bytes</span><span class="token punctuation">(</span>urllib<span class="token punctuation">.</span>parse<span class="token punctuation">.</span>urlencode<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;world&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
response <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlopen<span class="token punctuation">(</span><span class="token string">&quot;http://httpbin.org/post&quot;</span><span class="token punctuation">,</span>data<span class="token operator">=</span>data<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#超时处理</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
    response <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlopen<span class="token punctuation">(</span><span class="token string">&quot;ttp://httpbin.org/get&quot;</span><span class="token punctuation">,</span>timeout<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> urllib<span class="token punctuation">.</span>error<span class="token punctuation">.</span>URLError <span class="token keyword">as</span> e<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;time out!&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="常见dos命令" tabindex="-1"><a class="header-anchor" href="#常见dos命令" aria-hidden="true">#</a> 常见dos命令</h2><ul><li>dir 查看当前目录下的所有文件（夹）</li><li>cd进入导指定的目录 <ul><li>.表示当前目录</li><li>..表示上一级目录</li></ul></li><li>md 创建一个目录</li><li>rd 删除一个目录</li><li>del 删除一个文件</li><li>cls 清楚屏幕</li></ul><p>如果字符串里面有很多字符都需要转义，就需要加很多<code>\</code>，为了简化，Python还允许用<code>r&#39;&#39;</code>表示<code>&#39;&#39;</code>内部的字符串默认不转义</p><p>如果字符串内部有很多换行，用<code>\n</code>写在一行里不好阅读，为了简化，Python允许用<code>&#39;&#39;&#39;...&#39;&#39;&#39;</code>的格式表示多行内容</p><p>对于单个字符的编码，Python提供了<code>ord()</code>函数获取字符的整数表示，<code>chr()</code>函数把编码转换为对应的字符</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt;&gt;&gt; ord(&#39;A&#39;)
65
&gt;&gt;&gt; ord(&#39;中&#39;)
20013
&gt;&gt;&gt; chr(66)
&#39;B&#39;
&gt;&gt;&gt; chr(25991)
&#39;文&#39;
&#39;ABC&#39;.encode(&#39;ascii&#39;)
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于Python源代码也是一个文本文件，所以，当你的源代码中包含中文的时候，在保存源代码时，就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时，为了让它按UTF-8编码读取，我们通常在文件开头写上这两行：</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

第一行注释是为了告诉Linux/OS X系统，这是一个Python可执行程序，Windows系统会忽略这个注释；

第二行注释是为了告诉Python解释器，按照UTF-8编码读取源代码，否则，你在源代码中写的中文输出可能会有乱码。
print(f&#39;The area of a circle with radius {r} is {s:.2f}&#39;)
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div><!--[--><!--]--></div><footer class="page-meta"><div class="meta-item edit-link"><a class="external-link meta-item-label" href="https://gitee.com/Francis-xsc/francis-xsc/edit/main/program/python.md" rel="noopener noreferrer" target="_blank" aria-label="Edit this page"><!--[--><!--]--> Edit this page <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><div class="meta-item last-updated"><span class="meta-item-label">最后更新时间: </span><!----></div><div class="meta-item contributors"><span class="meta-item-label">Contributors: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: 920364365@qq.com">Francis-xsc</span><!----><!--]--><!--]--></span></div></footer><!----><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/assets/app-6c74e544.js" defer></script>
  </body>
</html>
